<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class DataUserController extends BaseController {
    /*
     * Konstruktor
     */

    public function __construct($registry) {
        parent::__construct($registry);
    }

    /*
     * Index
     */

    public function index() {
        $this->view->render('super/gantiUserOM');
    }
    
    public function getDataUser(){
        $d_user = new DataUser($this->registry);
        $filter = array();
        $no = 0;
		
		//untuk mencatat log user
        $d_log = new DataLog($this->registry);
		$d_log->set_activity_time_start(date("d-m-Y h:i:s"));
        
        
        $this->view->kppn_list = $d_user->get_kppn_kanwil(Session::get('id_user'));        

        if (isset($_POST['filter'])) {
            if ($_POST['f_username'] != '') {
                $filter[$no++] = "KD_SATKER = '" . $_POST['f_username'] . "'";
                $this->view->d_f_username = $_POST['f_username'];
            }
            
            if ($_POST['f_kewenangan'] != '') {
                $filter[$no++] = " KD_R_JENIS = '" . $_POST['f_kewenangan'] . "'";
                $this->view->d_f_kewenangan = $_POST['f_kewenangan'];
            }
            
            if ($_POST['f_dept'] != '') {
                $filter[$no++] = " KD_DEPT = '" . $_POST['f_dept'] . "'";
                $this->view->d_f_dept = $_POST['f_dept'];
            }
            
            if ($_POST['f_unit'] != '') {
                $filter[$no++] = " KD_UNIT = '" . $_POST['f_unit'] . "'";
                $this->view->d_f_unit = $_POST['f_unit'];
            }
            
            if ($_POST['f_kdkppn'] != 'SEMUAKPPN') {
                $filter[$no++] = " KD_D_KPPN = '" . $_POST['f_kdkppn'] . "'";
                $this->view->d_f_kdkppn = $_POST['f_kdkppn'];
            }
            
            if ($_POST['f_kanwil'] != '') {
                $filter[$no++] = "KD_KANWIL = '" . $_POST['f_kanwil'] . "'";
                $this->view->d_f_kanwil = $_POST['f_kanwil'];
            }
            
            if ($_POST['tgl_awal'] != '' AND $_POST['tgl_akhir'] != '') {
                $filter[$no++] = "TGL_DAFTAR BETWEEN TO_DATE ('" . date('Ymd', strtotime($_POST['tgl_awal'])) . "','YYYYMMDD') 
								AND TO_DATE ('" . date('Ymd', strtotime($_POST['tgl_akhir'])) . "','YYYYMMDD')  ";
                $this->view->d_tgl_awal = $_POST['tgl_awal'];
                $this->view->d_tgl_akhir = $_POST['tgl_akhir'];
            }
        } else {
            $filter[$no++] = "KD_SATKER = '" . Session::get('kd_satker') . "'";
            $this->view->d_f_username = Session::get('kd_satker');
        }
        
        $this->view->data = $d_user->get_d_user2($filter);
        
        $this->view->render('super/gantiUserOM');
		$d_log->tambah_log("Sukses");
    }

    /*
     * tambah data user
     */
    
    

    public function addDataUser($id = null) {
        $d_user = new DataUser($this->registry);
        if (isset($_POST['add_d_user'])) {
            $kd_r_jenis = $_POST['kd_r_jenis'];
            $kd_r_unit = $_POST['kd_r_unit'];
            $nama_user = $_POST['nama_user'];
            $pass_user = $_POST['pass_user'];
            $d_user->set_kd_r_jenis($kd_r_jenis);
            $d_user->set_kd_r_unit($kd_r_unit);
            $d_user->set_nama_user($nama_user);
            $d_user->set_pass_user($pass_user);
            if (!$d_user->add_d_user()) {
                $this->view->d_rekam = $d_user;
                $this->view->error = $d_user->get_error();
            }
        }
        if (!is_null($id)) {
            $d_user->set_kd_d_user($id);
            $this->view->d_ubah = $d_user->get_d_user_by_id($d_user);
        }
        $this->view->data = $d_user->get_d_user();
        $this->view->render('admin/dataUser');
    }

    /*
     * ubah data user
     */

    public function updDataUser() {
        $d_user = new DataUser($this->registry);
        if (isset($_POST['upd_d_user'])) {
            $kd_d_user = $_POST['kd_d_user'];
            $kd_r_jenis = $_POST['kd_r_jenis'];
            $kd_r_unit = $_POST['kd_r_unit'];
            $nama_user = $_POST['nama_user'];
            $pass_user = $_POST['pass_user'];
            $d_user->set_kd_d_user($kd_d_user);
            $d_user->set_kd_r_jenis($kd_r_jenis);
            $d_user->set_kd_r_unit($kd_r_unit);
            $d_user->set_nama_user($nama_user);
            $d_user->set_pass_user($pass_user);
            if (!$d_user->update_d_user()) {
                $this->view->d_ubah = $d_user;
                $this->view->error = $d_user->get_error();
                $this->view->data = $d_user->get_d_user();
                $this->view->render('admin/dataUser');
            } else {
                header('location:' . URL . 'dataUser/addDataUser');
            }
        }
    }

    /*
     * hapus data user
     * @param kd_d_user
     */

    public function delDataUser($id) {
        $d_user = new DataUser($this->registry);
        if (is_null($id)) {
            throw new Exception;
            echo "id belum dimasukkan!";
            return;
        }
        $d_user->set_kd_d_user($id);
        $d_user->delete_d_user();
        header('location:' . URL . 'dataUser/addDatauser');
    }

    /*
     * Destruktor
     */

    public function __destruct() {
        
    }

}
